package com.wzs.exceltotxt;

import com.wzs.exceltotxt.model.Device;
import com.wzs.exceltotxt.model.Station;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Application extends javafx.application.Application {
    protected static Map<String, String> map;

    @Override
    public void start(Stage stage) throws IOException {
        initData();

        FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 582, 360);
        stage.setTitle("excel转文本小工具");
        stage.setScene(scene);
        stage.show();
    }

    private void initData() {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Station.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

            //解析XML文件到Station对象
            Station station = (Station) unmarshaller.unmarshal(new File(System.getProperty("user.dir") + "/DevCode.xml"));
            List<Device> devices = station.getDevices();
            map = new HashMap<>();
            for (Device device : devices) {
                map.put(device.getName().toUpperCase(), device.getCode());
            }
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        launch();
    }
}